$(function(){
   showUsergroupList();
});

// 显示用户组信息
function showUsergroupList()
{
   var url = "user_group_power.php";
   var pars = { "page_status": 1 };
   $.post(url, pars, function(xmlobj){
	  $("#tbody2").html("");
	  $(xmlobj).find("item").each(function(i){
		 var sec_id = $(this).find("id").text();
		 var zhname = $(this).find("zhname").text();
		 var power = $(this).find("power").text();
		 $("<tr></tr>")
		   .append($("<th></th>").html(i + 1))
		   .append($("<td></td>").html(zhname))
		   .append($("<td></td>")
				   .html("<a href=\"javascript:void(0);\" id=\"view_" + sec_id + "\""
						 + " onclick=\"showPower(" + sec_id + ");\" power=\""
						 + power + "\">查看</a>"))
		   .append($("<td></td>")
				   .html("<a href=\"javascript:void(0);\""
						 + " onclick=\"modifyPower(" + sec_id + ");\">修改</a>"))
		   .appendTo("#tbody2");
	  });
	  tablePretty("#tbody2");
   });
}

// 显示部门列表
function showSectionList(comid)
{
   var url = "user_group_power.php";
   var pars = { "page_status": 2,
		"company_id": comid
   };
   $("#tbody2").html("");
   $.post(url, pars, function(xmlobj){
	  $(xmlobj).find("item").each(function(){
		 var sec_id = $(this).find("id").text();
		 var zhname = $(this).find("zhname").text();
		 var power = $(this).find("power").text();
		 $("<tr></tr>").append($("<td></td>").html(zhname))
		   .append($("<td></td>")
				   .html("<a href=\"javascript:void(0);\" id=\"view_" + sec_id + "\""
						 + " onclick=\"showPower(" + sec_id + ");\" power=\""
						 + power + "\">查看</a>"))
		   .append($("<td></td>")
				   .html("<a href=\"javascript:void(0);\""
						 + " onclick=\"modifyPower(" + sec_id + ");\">修改</a>"))
		   .appendTo("#tbody2");
	  });
   });
}

// 显示权限
function showPower(sec_id)
{
   var power_array = $("#view_" + sec_id).attr("power").split(",").sort();
   var power_list = $("<div></div>")
	 .addClass("power_list")
	 .append($("<div></div>").html("现有权限："))
	 .append(getPowerListViaJs(power_array));

   $.modal(power_list, {
	  onShow: function()
		{
		   $("#company_list").find("input").css({visibility: "hidden"});
		},
	  onClose: function()
		{
		   $("#company_list").find("input").css({visibility: "visible"});
		   $.modal.close();
		}
   });
   // alert("Power: " + sec_id);
}

// 修改权限
function modifyPower(sec_id)
{
	var full = [];
	$.each(__powerArray__, function(i, n) {
		full[full.length] = __powerArray__[i][3];
	});
	var power_array = $("#view_" + sec_id).attr("power").split(",").sort();
	var power_list = $("<div></div>")
		.append($("<div></div>").addClass("modal_header").html("修改权限"))
		.append($("<div></div>").addClass("modal_content").append(makePowerCheckBox(full, power_array, 'power_check', 'pc[]')))
		.append($("<div></div>").addClass("modal_footer")
				.append($("<input type=\"button\" />").attr("sid", sec_id).val("提交")
					.click(function(){
						var powerlist = [];
						$("#power_check input:checkbox[@name='pc\[\]']:checked").each(function(i){
							powerlist[powerlist.length] = $(this).val();
						});
						var url = "user_group_power.php";
						var sid = $(this).attr("sid");
						var pars = { page_status: 2, sid: sid, powerlist: powerlist.join(",") };
						$.post(url, pars, function(text) {
							if (/^Success/.test(text)) { $("#view_" + sid).attr("power", pars["powerlist"]); $.modal.close(); }
						});
					})
				));
   $.modal(power_list);
}
